﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using Meraz.Core.Config;
using System.Diagnostics;

namespace Meraz.Core.Parsers
{
    public class LinkParser
    {
        #region other link attributes
        //static readonly List<string> attributes = new List<string>
        //{
        //    "accesskey",
        //    "charset",
        //    "class",
        //    "coords",
        //    "dir",
        //    "hreflang",
        //    "id",
        //    "lang",
        //    "name",
        //    "onblur",
        //    "onclick",
        //    "onfocus",
        //    "onmousedown",
        //    "onmousemove",
        //    "onmouseout",
        //    "onmouseover",
        //    "onmouseup",
        //    "onkeydown",
        //    "onkeypress",
        //    "onkeyup",
        //    "rel",
        //    "rev",
        //    "shape",
        //    "style",
        //    "tabindex",
        //    "target",
        //    "title",
        //    "xml:lang"
        //};
        #endregion
        char[] attrMarkers = new char[] { ' ', '>' };
        const string linkPattern = @"(<a.*?href=(?<HREF>(?<QUOTE>.).*?)>(?<TEXT>.*?)</a>)";
        Regex linkRegex = null;
        public LinkParser() { linkRegex = new Regex(linkPattern, RegexOptions.IgnoreCase); }
        
        public List<string[]> Parse(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
                throw new ApplicationException("Cannot parse empty text");

            List<string[]> results = new List<string[]>();
            MatchCollection linkMatches = linkRegex.Matches(text);
            foreach (Match linkMatch in linkMatches)
            {
                if (linkMatch.Success)
                {
                    string linkName = linkMatch.Groups["TEXT"].Value.Trim();
                    string hrefDirty = linkMatch.Groups["HREF"].Value.Trim();
                    string quote = linkMatch.Groups["QUOTE"].Value;
                    string href = ParseHref(hrefDirty, quote);
                    string[] pair = new string[2];
                    pair[0] = href ?? string.Empty;
                    pair[1] = linkName ?? string.Empty;
                    results.Add(pair);
                }
            }
            return results;
        }
        string ParseHref(string blob, string quote)
        {
            int start = 0, end = 0;
            switch (quote)
            {
                case @"""":
                case "'":
                    start = 1;
                    end = blob.IndexOf(quote, start);
                    break;
                default:
                    end = blob.IndexOfAny(attrMarkers);
                    break;
            }
            string result = (end < start)?
                blob.Substring(start) :
                blob.Substring(start, end - start);
            return result.Trim('"', '\'', ' ');
        }
    }
}
